//
//  TTBottomView.swift
//  TTWeibo
//
//  Created by 陶宏路 on 16/7/26.
//  Copyright © 2016年 陶路路. All rights reserved.

import UIKit

enum BottomViewType:Int{
    
    case Picture = 10000
    case Mention = 10001
    case Trend = 10002
    case Emoticon = 10003
    case Add = 10004
}

class TTBottomView: UIView {
    
    var buttonBlock:((type:BottomViewType)->())?
    
    var emoticonButton:UIButton?

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        backgroundColor = UIColor.whiteColor()
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI(){
        
        let pictureButton = getButton("compose_toolbar_picture",type: .Picture)
        let mentionButton = getButton("compose_mentionbutton_background",type: .Mention)
        let trendButton = getButton("compose_trendbutton_background",type: .Trend)
        let emoticonButton = getButton("compose_emoticonbutton_background",type: .Emoticon)
        let addButton = getButton("compose_add_background",type: .Add)
        self.emoticonButton = emoticonButton
        pictureButton.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        
        mentionButton.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(pictureButton)
            make.leading.equalTo(pictureButton.snp_trailing)
            make.width.equalTo(trendButton)
        }
        
        trendButton.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(pictureButton)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emoticonButton)
        }
        
        emoticonButton.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(pictureButton)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(addButton)

        }
        
        addButton.snp_makeConstraints { (make) -> Void in
            
            make.trailing.top.bottom.equalTo(self)
            make.leading.equalTo(emoticonButton.snp_trailing)
            make.width.equalTo(pictureButton)

        }
    }
    
    
    func didBottomButton(sender:UIButton){
        
        buttonBlock?(type: BottomViewType(rawValue: sender.tag)!)
    }
    
    var isEmoji:Bool = false{
        
        didSet{
            
            let setImage = isEmoji ? "compose_keyboardbutton_background" : "compose_emoticonbutton_background"
            
            emoticonButton?.setImage(UIImage(named: setImage), forState: UIControlState.Normal)
            emoticonButton?.setImage(UIImage(named: "\(setImage)_highlighted"), forState: UIControlState.Highlighted)
        }
    }
    

}

extension TTBottomView{
    
    func getButton(img:String,type:BottomViewType)->UIButton{
        let btn = UIButton()
        btn.setImage(UIImage(named: img), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "\(img)_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        btn.addTarget(self, action: "didBottomButton:", forControlEvents: UIControlEvents.TouchUpInside)
        btn.tag = type.rawValue
        addSubview(btn)
        return btn
    }
    
}